/**
 * FileSystemStoreAdaptor - Adapt a File System store to a UPNP Content Directory
 *
 * @author Scot McSweeney-Roberts
 * @version 0.1
 * 
 */

package uk.co.sheitgeist.fums.server.upnp.adaptors

import uk.co.sheitgeist.fums.server.content.store.*
import uk.co.sheitgeist.fums.server.content.store.filesystem.*
import com.cidero.upnp.*
import java.util.logging.Logger;


public class FileSystemStoreAdaptor extends StoreAdaptor {

	private static Logger logger = Logger.getLogger("uk.co.sheitgeist")

	private String storeContext
	private FileSystemStore store

	public FileSystemStoreAdaptor(String storeContext, Store store) {
		this.storeContext = storeContext
		this.store = (FileSystemStore) store
	}

	/**
	 * return the root of this store as a CDSObject
	 *
	 */
	public CDSObject toCDSObject() {
		CDSStorageFolder folder = new CDSStorageFolder()
		folder.id = "${this.storeContext}:${store.rootId}"
		folder.parentId = "0"
		folder.title = this.storeContext
		folder.creator = "UNKNOWN"
		return (CDSObject)folder
	}

	public CDSObjectList getDirectChildren(String objectId) {
		logger.info "FSSA::getDirectCholdren, start - ${objectId}"
			
		CDSObjectList children = new CDSObjectList()
		StoreNode object = this.store.getNodeById(objectId)
		object.children.each {child ->
			logger.info "FSSA::getDirectCholdren - ${child.toString()}"
			if (child.isDirectory) {
				CDSStorageFolder folder = new CDSStorageFolder()
				folder.id = "${this.storeContext}:${child.id}"
				folder.parentId = "${this.storeContext}:${objectId}"
				folder.title = child.name
				folder.creator = "UNKNOWN"
				children.add(folder)
			} else {
				switch (child.primaryType) {
					case "image": break;
					case "audio": 
						CDSAudioItem audioItem = new CDSAudioItem()
						audioItem.id = "${this.storeContext}:${child.id}"
						audioItem.parentId = "${this.storeContext}:${objectId}"
						audioItem.title = child.name
						audioItem.creator = "UNKNOWN"
						children.add(audioItem)
						break;
					default: logger.info "FSSA::getDirectChildren - primaryType - ${child.primaryType}"
					logger.info "FSSA::getDirectChildren - subType - ${child.subType}"
				}
			}
		}
		return children
	}
}
